package com.miplus.ccrm.core.service;

import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.conf.WfNextStepVo;
import com.miplus.ccrm.core.pojo.WfSteps;
import com.miplus.ccrm.core.service.conf.MsgAlertsService;
import com.miplus.ccrm.core.service.conf.WfCaseactionsService;
import com.miplus.ccrm.core.service.conf.WfCasesService;
import com.miplus.ccrm.core.service.conf.WfStepsService;
import com.miplus.ccrm.core.utils.EnumConfirmStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class FiWorkFlowCase {
    @Resource
    private WfCasesService wfCasesService;
    @Resource
    private WfStepsService wfStepsService;
    @Resource
    private WfCaseactionsService wfCaseactionsService;

    public String doCreate(String $usrid,Integer wfid,Integer originalid,Integer tasknumber)
    { //返回工作流对应的状态
        WfSteps wfSteps=wfStepsService.getFirstStepInfo(wfid);
        if(wfSteps==null){
            log.info("CreateWorkFlowCase:getFirstStepInfo({}) isnull",wfid);
            throw new ServiceException(EnumRCode.WfNoStartStep.value, EnumRCode.WfNoStartStep.name);
        }
        int wfcaseid=wfCasesService.wfCaseAdd($usrid,wfid,originalid);
        log.info("CreateWorkFlowCase:wfid={},originalid={},tasknumber={},wfcaseid={},Stepid={},status={}",
                wfid,originalid,tasknumber,wfcaseid,wfSteps.getStepid(),wfSteps.getStatus()
        );
        wfCaseactionsService.wfActionAdd($usrid,wfcaseid,
                wfSteps.getStepid()
        );
        return wfSteps.getStatus();
    }

    public WfNextStepVo doExec(String $usrid, Integer caseactid, Integer actionid, Integer wfcaseid, String remark)
    {
        if(JPubUtils.IsEmpty(wfcaseid)){
            wfcaseid=wfCaseactionsService.getWfCaseID(caseactid);
        }
        if(JPubUtils.IsEmpty(wfcaseid)||wfcaseid<=0){
            log.warn("ExecWorkFlowCase.nofind wfcaseid.");
            return null;
        }
        WfNextStepVo wfNextStepVo=wfStepsService.getStepInfoByAction(actionid);
        if(wfNextStepVo==null){
            log.info("ExecWorkFlowCase:getStepInfoByAction({}) isnull",actionid);
            throw new ServiceException(EnumRCode.WfNoNextStep.value, EnumRCode.WfNoNextStep.name);
        }
        log.info("ExecWorkFlowCase:caseactid={},actionid={},wfcaseid={},NextStepid={},status={},type={}",
                caseactid,actionid,wfcaseid,wfNextStepVo.getNextstepid(),
                wfNextStepVo.getStatus(),wfNextStepVo.getType()
        );
        wfCaseactionsService.wfActionUpdate($usrid,caseactid,actionid,remark);
        if(JPubUtils.IsNotEmpty(wfNextStepVo.getNextstepid())){
            wfCaseactionsService.wfActionAdd($usrid,wfcaseid,wfNextStepVo.getNextstepid(),caseactid);
        }
        //工作流节点类型：WF.STEP.TYPE
        //1：开始
        //2：中间
        //3：结束
        if(JPubUtils.IsEqual(wfNextStepVo.getType(),"3")){
            wfCasesService.wfCaseClose($usrid,wfcaseid);
        }

        return wfNextStepVo;
    }

    public void doCancel(String $usrid,Integer wfid,Integer originalid)
    {
        log.info("CancelWorkFlowCase:wfid={},originalid={}", wfid,originalid);
        if(wfid>0) {//有工作流
            wfCasesService.wfCaseCancel($usrid, wfid, originalid);
        }
    }

    public String getPassStatus(String status)
    {
        if(JPubUtils.IsEqual(status, EnumConfirmStatus.Pass.value))
            return "P";
        else return "E";
    }
}
